package kq

import (
	"context"
	"encoding/json"
	"strconv"

	// "mall/common/errorx"
	"mall/common/globalkey"
	"mall/common/mqueuex"
	"mall/common/utils/pointy"
	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/backend/rmq/internal/svc"
	"mall/service/backend/rpc/backendclient"
	"mall/service/blog/rpc/blogclient"
	"mall/service/forum/rpc/forumclient"
	"mall/service/product/rpc/productclient"
	"mall/service/promotion/rpc/promotionclient"
	"mall/service/user/rpc/userclient"

	// "github.com/pkg/errors"
	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type AttachmentSyncQiniuMq struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAttachmentSyncQiniuMq(ctx context.Context, svcCtx *svc.ServiceContext) *AttachmentSyncQiniuMq {
	return &AttachmentSyncQiniuMq{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AttachmentSyncQiniuMq) Consume(_, val string) error {
	// logrus.Info(fmt.Sprintf("AttachmentSyncQiniuMq Consume val: %+v", val))
	// {"id":49549,"put":1,"watermark":0,"thumbnail":0}

	// 解析数据
	var message mqueuex.AttachmentSyncQiniuMessage
	if err := json.Unmarshal([]byte(val), &message); err != nil {
		// logx.WithContext(l.ctx).Error("AttachmentSyncQiniuMq Consume json.Unmarshal err : %+v , val : %s ", err, val)
		logx.Errorw("AttachmentSyncQiniuMq Consume json.Unmarshal err", logx.Field("detail", err.Error()))
		return err
	}

	// 执行业务..
	if err := l.execService(&message); err != nil {
		// logx.WithContext  Context信息
		// logx.WithContext(l.ctx).Error("AttachmentSyncQiniuMq execService err : %v , val : %s , message: %+v ", err, val, message)
		logx.Errorw("AttachmentSyncQiniuMq execService err", logx.Field("detail", err.Error()))
		// 附加上下文
		// consume: {\"aid\":45170,\"cdn\":1,\"watermark\":1,\"thumbnail\":0}
		return err
	}

	return nil
}

func (l *AttachmentSyncQiniuMq) execService(message *mqueuex.AttachmentSyncQiniuMessage) error {
	// logrus.Info(fmt.Sprintf("AttachmentSyncQiniuMq execService message: %+v", message))
	// {Id:49549 Put:1 Watermark:0 Thumbnail:0}

	if message.Id == 0 {
		return nil
	}

	if message.Put > 0 {
		_, err := l.svcCtx.AttachmentRpc.QiniuPutFile(l.ctx, &attachmentclient.RecordInfo{
			Id: pointy.GetPointer(strconv.FormatUint(message.Id, 10)),
		})
		if err != nil {
			return err
		}

		return nil

		// res, err := l.svcCtx.AttachmentRpc.SyncQiniu(l.ctx, &attachmentclient.SyncQiniuReq{
		// 	Id:        message.Id,
		// 	Put:       message.Put,
		// 	Watermark: message.Watermark,
		// 	Thumbnail: message.Thumbnail,
		// })
		// if err != nil {
		// 	// return errors.Wrapf(errorx.NewDefaultError("kq Attachment Sync Qiniu fail"), "kq Attachment Sync Qiniu fail, err: %v , message: %+v ", err, message)
		// 	return errors.New("AttachmentSyncQiniuMq execService SyncQiniu error")
		// }

		// if res.Url != "" {
		// 	_, err = l.svcCtx.BlogRpc.SyncQiniu(l.ctx, &blogclient.SyncQiniuReq{
		// 		Id:  message.Id,
		// 		Url: res.Url,
		// 		Alt: res.Alt,
		// 	})
		// 	if err != nil {
		// 		// errors.Wrapf  更多错误信息
		// 		// {"@timestamp":"2023-05-08T03:37:01.165+08:00","caller":"kq/queue.go:161","content":"consume: {\"aid\":45170,\"cdn\":1,\"watermark\":1,\"thumbnail\":0}, error: kq Blog Sync Qiniu fail, err: rpc error: code = DeadlineExceeded desc = context deadline exceeded , message: {Id:45170 Cdn:1 Watermark:1 Thumbnail:0}: errorCode: 10001, errorMessage: kq Blog Sync Qiniu fail","level":"error"}
		// 		return errors.Wrapf(errorx.NewDefaultError("kq Blog Sync Qiniu fail"), "kq Blog Sync Qiniu fail, err: %v , message: %+v", err, message)
		// 		// 附加原始错误信息和参数
		// 		// rpc error: code = DeadlineExceeded desc = context deadline exceeded
		// 		// {Id:45170 Put:1 Watermark:1 Thumbnail:0}
		// 	}

		// 	_, err = l.svcCtx.ForumRpc.SyncQiniu(l.ctx, &forumclient.SyncQiniuReq{
		// 		Id:  int64(message.Id),
		// 		Url: res.Url,
		// 		Alt: res.Alt,
		// 	})
		// 	if err != nil {
		// 		// errors.Wrapf  更多错误信息
		// 		return errors.Wrapf(errorx.NewDefaultError("kq Forum Sync Qiniu fail"), "kq Forum Sync Qiniu fail, err: %v , message: %+v", err, message)
		// 		// 附加原始错误信息和参数
		// 		// rpc error: code = DeadlineExceeded desc = context deadline exceeded
		// 		// {Id:45170 Put:1 Watermark:1 Thumbnail:0}
		// 	}
		// }
	}

	if message.Watermark > 0 {
		_, err := l.svcCtx.AttachmentRpc.QiniuWatermark(l.ctx, &attachmentclient.RecordInfo{
			Id: pointy.GetPointer(strconv.FormatUint(message.Id, 10)),
		})
		if err != nil {
			return err
		}

		return nil
	}

	if message.Thumbnail > 0 {
		_, err := l.svcCtx.AttachmentRpc.QiniuThumbnail(l.ctx, &attachmentclient.RecordInfo{
			Id: pointy.GetPointer(strconv.FormatUint(message.Id, 10)),
		})
		if err != nil {
			return err
		}

		return nil
	}

	if message.Type > 0 && len(message.Url) > 0 {
		switch message.Type {
		case globalkey.AttachmentLogTypePost, globalkey.AttachmentLogTypePage,
			globalkey.AttachmentLogTypeTerm, globalkey.AttachmentLogTypeKnowledgeBase:
			_, err := l.svcCtx.BlogRpc.SyncQiniu(l.ctx, &blogclient.SyncQiniuReq{
				Id:      message.TypeId,
				MediaId: message.Id,
				Url:     message.Url,
			})
			if err != nil {
				return err
			}
		case globalkey.AttachmentLogTypeForumTerm, globalkey.AttachmentLogTypeForumPost,
			globalkey.AttachmentLogTypeForumReview, globalkey.AttachmentLogTypeForumTopic,
			globalkey.AttachmentLogTypeForumAvatar, globalkey.AttachmentLogTypeWardrobe:
			_, err := l.svcCtx.ForumRpc.SyncQiniu(l.ctx, &forumclient.SyncQiniuReq{
				Id:      message.TypeId,
				MediaId: message.Id,
				Url:     message.Url,
			})
			if err != nil {
				return err
			}
		case globalkey.AttachmentLogTypeProductTerm, globalkey.AttachmentLogTypeProduct:
			_, err := l.svcCtx.ProductRpc.SyncQiniu(l.ctx, &productclient.SyncQiniuReq{
				Id:      message.TypeId,
				MediaId: message.Id,
				Url:     message.Url,
			})
			if err != nil {
				return err
			}
		case globalkey.AttachmentLogTypePromotion, globalkey.AttachmentLogTypePromotionCoupon:
			_, err := l.svcCtx.PromotionRpc.SyncQiniu(l.ctx, &promotionclient.SyncQiniuReq{
				Id:      message.TypeId,
				MediaId: message.Id,
				Url:     message.Url,
			})
			if err != nil {
				return err
			}
		case globalkey.AttachmentLogTypeUserAvatar:
			_, err := l.svcCtx.UserRpc.SyncQiniu(l.ctx, &userclient.SyncQiniuReq{
				Id:      message.TypeId,
				MediaId: message.Id,
				Url:     message.Url,
			})
			if err != nil {
				return err
			}
		case globalkey.AttachmentLogTypeBanner, globalkey.AttachmentLogTypeSection:
			_, err := l.svcCtx.BackendRpc.SyncQiniu(l.ctx, &backendclient.SyncQiniuReq{
				Id:      message.TypeId,
				MediaId: message.Id,
				Url:     message.Url,
			})
			if err != nil {
				return err
			}
		}
	}

	return nil
}
